package digfinderserver;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.thoughtworks.xstream.XStream;

// POJO, no interface no extends

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/digfinder")
public class DigFinder {


	// Defines that the next path parameter after todos is
		// treated as a parameter and passed to the TodoResources
		// Allows to type http://localhost:8080/de.vogella.jersey.todo/rest/todos/1
		// 1 will be treaded as parameter todo and passed to TodoResource
		@Path("{query}")
		@GET
		@Produces(MediaType.APPLICATION_JSON)
		public String search( @PathParam("query") String query) {
		
			
			Scraper scraper = new Scraper();
			Gson gson = new Gson();
			String json;
			
			//result of search
			List<Result_Item> result_items = scraper.Search(query);

			//print out search results
		/*	String searchRes = "" ;
			for(int i=0; i<result_items.size(); i++){								
				searchRes = searchRes + result_items.get(i).getTitle() + "\n\n";
				
			} 
		*/
			
			//convert search results to json as string
			json = gson.toJson(result_items);
						
			return json; 
			
								
			
		}
	
			
	// This method is called if XML is request
	@Path("{query}")
	@GET
	@Produces(MediaType.TEXT_XML)
	public String search1( @PathParam("query") String query) {
	
		
		Scraper scraper = new Scraper();
		
		//result of search
		List<Result_Item> result_items = scraper.Search(query);
	
		XStream xstream = new XStream();
		String xml = xstream.toXML(result_items);
		
		return xml;
	
	}
/*
	// This method is called if HTML is request
	@GET
	@Produces(MediaType.TEXT_HTML)
	public String sayHtmlHello() {
		return "<html> " + "<title>" + "Hello Jersey" + "</title>"
				+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
	}
*/
}